package com.Thread;

/**
 * @ClassName Demo1
 * @Description 在三原色中,使用多线程实现,分别随机打印输出红、绿、蓝三个随机值
 * @Author Kx
 * @Date 2025/3/7 10:01
 * @Version 1.0
 */
public class Demo1 {
    public static void main(String[] args) {
        ThreadDemo1 threadDemo1 = new ThreadDemo1();

        // 创建三个线程
        Thread thread1 = new Thread(threadDemo1);
        // 设置线程名称
        thread1.setName("red");
        thread1.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Thread thread2 = new Thread(threadDemo1);
        thread2.setName("green");
        thread2.start();
        try {
            thread2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Thread thread3 = new Thread(threadDemo1);
        thread3.start();
        thread3.setName("blue");
        try {
            thread3.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

class ThreadDemo1 implements Runnable{
    @Override
    public void run() {
        // 获取随机数
        int red = (int)(Math.random() * 256);
        System.out.println(Thread.currentThread().getName() + ":" + red);
    }
}
